#include "accountmanager.h"

AccountManager::AccountManager(QObject *parent) :
    QObject(parent)
{
}

void AccountManager::addAccount(XMPPClient *client)
{
    accounts.insert(client->jid(), client);
}

bool AccountManager::removeAccount(gloox::JID jid)
{
    int count = accounts.remove(jid);
    if (count)
        return true;
    else
        return false;
}

bool AccountManager::removeAccount(XMPPClient *client)
{
    for(QMap<gloox::JID, XMPPClient*>::iterator i = accounts.begin(); i != accounts.end(); i++)
    {
        if (i.value() == client)
        {
            accounts.remove(i.key());
            return true;
        }
    }
    return false;
}

XMPPClient* AccountManager::findAccount(gloox::JID jid)
{
    auto i = accounts.find(jid);
    if (i != accounts.end())
        return i.value();
    else
        return NULL;
}

void AccountManager::disconnectAll()
{
    for(QMap<gloox::JID, XMPPClient*>::iterator i = accounts.begin(); i != accounts.end(); i++)
    {
        i.value()->disconnect();
    }
}
